/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { crypto } from '@base/utils/encryption';
import request from '@base/utils/request';
import { isFunction } from 'lodash';

type UmiRequest = Window['umiRequest'];
type UmiCrypto = Window['umiCrypto'];

const umiRequest: UmiRequest = (opts) => {
  const { url, beforeSend, success, complete, error, type, ...rest } = opts;
  if (isFunction(beforeSend)) {
    beforeSend();
  }
  return request(url, { method: type || 'POST', ...rest })
    .then(success)
    .catch(error)
    .finally(complete);
};

const umiCrypto: UmiCrypto = (opts) => {
  const { success, error, ...rest } = opts;
  return crypto(rest).then(success).catch(error);
};

window.umiCrypto = umiCrypto;
window.umiRequest = umiRequest;
